class Solutoin02{
	public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
		if(root == null || p == null){
			return null;
		}
		Deque<TreeNode> stack = new ArrayDeque<>();
		TreeNode curNode = root;
		boolean found = false;
		while(!stack.isEmpty() || curNode != null){
			while(curNode != null){
				stack.offerLast(curNode);
				curNode = curNode.left;
			}
			curNode = stack.pollLast();
			if(found){
				break;
			} else if(curNode.val == p.val){
				// found为true后，还会中序遍历一个节点
				found = true;
			}
			curNode = curNode.right;
		}
		return curNode;
	}
}
